package com.sjb.mianshiya.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjb.mianshiya.mapper.common.CommonConstant;
import com.sjb.mianshiya.model.domain.QuestionBank;
import com.sjb.mianshiya.model.domain.User;
import com.sjb.mianshiya.model.request.questionBank.QuestionBankQueryRequest;
import com.sjb.mianshiya.model.vo.QuestionBankVO;
import com.sjb.mianshiya.service.QuestionBankService;
import com.sjb.mianshiya.mapper.QuestionBankMapper;
import com.sjb.mianshiya.service.UserService;
import com.sjb.mianshiya.utils.SqlUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author dell
 * @description 针对表【question_bank(题库)】的数据库操作Service实现
 * @createDate 2024-09-17 15:40:16
 */
@Service
public class QuestionBankServiceImpl extends ServiceImpl<QuestionBankMapper, QuestionBank>
        implements QuestionBankService {

    @Resource
    private QuestionBankMapper questionBankMapper;
    ;

    @Resource
    private UserService userService;

    @Override
    public void validQuestionBank(QuestionBank questionBank, boolean b) {

    }

    @Override
    public QuestionBankVO getQuestionBankVO(QuestionBank questionBank, HttpServletRequest request) {
        return QuestionBankVO.objToVo(questionBank);
    }

    @Override
    public Wrapper<QuestionBank> getQueryWrapper(QuestionBankQueryRequest questionBankQueryRequest) {
        QueryWrapper<QuestionBank> queryWrapper = new QueryWrapper<>();
        if (questionBankQueryRequest == null) {
            return queryWrapper;
        }
        // todo 从对象中取值
        Long id = questionBankQueryRequest.getId();
        Long notId = questionBankQueryRequest.getNoteId();
        String title = questionBankQueryRequest.getTitle();
        String searchText = questionBankQueryRequest.getSearchText();
        String sortField = questionBankQueryRequest.getSortField();
        String sortOrder = questionBankQueryRequest.getSortOrder();
        Long userId = questionBankQueryRequest.getUserId();
        String description = questionBankQueryRequest.getDescription();
        String picture = questionBankQueryRequest.getPicture();

        // todo 补充需要的查询条件
        // 从多字段中搜索
        if (StringUtils.isNotBlank(searchText)) {
            // 需要拼接查询条件
            queryWrapper.and(qw -> qw.like("title", searchText).or().like("description", searchText));
        }
        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
        queryWrapper.like(StringUtils.isNotBlank(description), "description", description);
        // 精确查询
        queryWrapper.ne(ObjectUtils.isNotEmpty(notId), "id", notId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "userId", userId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(picture), "picture", picture);
        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    @Override
    public Page<QuestionBankVO> getQuestionBankVOPage(Page<QuestionBank> questionBankPage, HttpServletRequest request) {
        List<QuestionBank> questionBankPageRecords = questionBankPage.getRecords();
        Page<QuestionBankVO> questionBankVoPage = new Page<>(questionBankPage.getCurrent(),
                questionBankPage.getSize(), questionBankPage.getTotal());
        if (CollUtil.isEmpty(questionBankPageRecords)) {
            return questionBankVoPage;
        }
        List<QuestionBankVO> questionBankVOList = questionBankPageRecords.stream().map(questionBankVo -> {
            return QuestionBankVO.objToVo(questionBankVo);
        }).collect(Collectors.toList());

        // 关联查询用户信息
        Set<Long> userIdSet = questionBankVOList.stream().map(QuestionBankVO::getUserId).collect(Collectors.toSet());
        List<User> users = userService.listByIds(userIdSet);
        Map<Long, List<User>> userListMap = users.stream().collect(Collectors.groupingBy(User::getId));

        questionBankVOList.forEach(questionBankVo -> {
            Long userId = questionBankVo.getUserId();
            User user = null;
            if (userListMap.containsKey(userId)) {
                user = userListMap.get(userId).get(0);
            }
            questionBankVo.setUser(userService.getUserVo(user));
        });
        questionBankVoPage.setRecords(questionBankVOList);
        return questionBankVoPage;
    }
}




